﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Admin
{
	public partial class tbReadingConfig : System.Web.UI.Page
	{
		static string Id = "";
		static string Lang = "";
        
		protected void Page_Load(object sender, EventArgs e)
		{
            Lang = Global.GetLang();
			if (!IsPostBack)
			{
				lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
				lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
				NumberClass.OnlyInputNumber(txtRepeat);
				NumberClass.OnlyInputNumber(txtord);
				Lang = Session["Lang"].ToString();
                LoadFilterStatesDropDownList();               
				BindGrid();
			}
		}

		private void BindGrid()
		{
            //string strWhere = " rid = '" + Lang + "'";
            string strWhere = string.Empty;
            if (Common.StringClass.Check(ddlFilterStates.SelectedValue))
            {
                strWhere += " staid = '" + ddlFilterStates.SelectedValue + "' ";
            }           

            grdtbReadingConfig.DataSource = tbReadingConfigService.tbReadingConfig_GetByTop("",strWhere,"");
            grdtbReadingConfig.DataBind();
            if (grdtbReadingConfig.PageCount <= 1)
            {
                grdtbReadingConfig.PagerStyle.Visible = false;
            }
		}

        private void LoadFilterStatesDropDownList()
        {
            ddlFilterStates.Items.Clear();
            ddlFilterStates.Items.Add(new ListItem(" -- Tất cả -- ", ""));
            List<Data.tbStates> list = tbStatesService.tbStates_GetByAll();
            for (int i = 0; i < list.Count; i++)
            {
                ddlFilterStates.Items.Add(new ListItem(list[i].stateName, list[i].staId));
            }
            ddlFilterStates.DataBind();            
        }        

		private void LoadtbStatesDropDownList()
		{
			ddltbStates.Items.Clear();
			ddltbStates.Items.Add(new ListItem("", ""));
			List<Data.tbStates> list = tbStatesService.tbStates_GetByAll();
			for (int i = 0; i < list.Count; i++)
			{
				ddltbStates.Items.Add(new ListItem(list[i].stateName, list[i].staId));
			}
			ddltbStates.DataBind();
		}

		protected void grdtbReadingConfig_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			ListItemType itemType = e.Item.ItemType;
			if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
			{
				if (itemType == ListItemType.Header)
				{
					object checkBox = e.Item.FindControl("chkSelectAll");
					if ((checkBox != null))
					{
						((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
					}
				}
				else
				{
					string tableRowId = grdtbReadingConfig.ClientID + "_row" + e.Item.ItemIndex.ToString();
					e.Item.Attributes.Add("id", tableRowId);
					object checkBox = e.Item.FindControl("chkSelect");
					if ((checkBox != null))
					{
						e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
						e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
						((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
					}
				}
			}
		}

		protected void grdtbReadingConfig_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
		{
			grdtbReadingConfig.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		protected void grdtbReadingConfig_ItemCommand(object source, DataGridCommandEventArgs e)
		{
			string strCA = e.CommandArgument.ToString();
			switch (e.CommandName)
			{
				case "Edit":
					Id = strCA;
					List<Data.tbReadingConfig> listE = tbReadingConfigService.tbReadingConfig_GetById(Id);
					txtrid.Text = listE[0].rid;
					txtstartTitleString.Text = listE[0].startTitleString;
					chkstartTitlePoint.Checked = listE[0].startTitlePoint == "1" || listE[0].startTitlePoint == "True";
					txtendTitleString.Text = listE[0].endTitleString;
					chkendTitlePoint.Checked = listE[0].endTitlePoint == "1" || listE[0].endTitlePoint == "True";
					txtstartJackpotString.Text = listE[0].startJackpotString;
					chkstartJackpotPoint.Checked = listE[0].startJackpotPoint == "1" || listE[0].startJackpotPoint == "True";
					txtendJackpotString.Text = listE[0].endJackpotString;
					chkendJackpotPoint.Checked = listE[0].endJackpotPoint == "1" || listE[0].endJackpotPoint == "True";
					txtRepeat.Text = listE[0].Repeat;
					txtord.Text = listE[0].ord;
					LoadtbStatesDropDownList();
					ddltbStates.Text = listE[0].staid;
					pnView.Visible = false;
					pnUpdate.Visible = true;
					break;
				case "Delete":
					tbReadingConfigService.tbReadingConfig_Delete(strCA);
					BindGrid();
					break;
			}
		}

		protected void AddButton_Click(object sender, EventArgs e)
		{
			pnUpdate.Visible = true;
			ControlClass.ResetControlValues(this);
			LoadtbStatesDropDownList();
			pnView.Visible = false;
		}

		protected void DeleteButton_Click(object sender, EventArgs e)
		{
			DataGridItem item = default(DataGridItem);
			for (int i = 0; i < grdtbReadingConfig.Items.Count; i++)
			{
				item = grdtbReadingConfig.Items[i];
				if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
				{
					if (((CheckBox)item.FindControl("ChkSelect")).Checked)
					{
						string strId = item.Cells[1].Text;
						tbReadingConfigService.tbReadingConfig_Delete(strId);
					}
				}
			}
			grdtbReadingConfig.CurrentPageIndex = 0;
			BindGrid();
		}

		protected void RefreshButton_Click(object sender, EventArgs e)
		{
			BindGrid();
		}

		protected void Update_Click(object sender, EventArgs e)
		{
			if (Page.IsValid){
				Data.tbReadingConfig obj = new Data.tbReadingConfig();
				obj.rid = Id;
				obj.startTitleString = txtstartTitleString.Text;
				obj.startTitlePoint = chkstartTitlePoint.Checked ? "1" : "0";
				obj.endTitleString = txtendTitleString.Text;
				obj.endTitlePoint = chkendTitlePoint.Checked ? "1" : "0";
				obj.startJackpotString = txtstartJackpotString.Text;
				obj.startJackpotPoint = chkstartJackpotPoint.Checked ? "1" : "0";
				obj.endJackpotString = txtendJackpotString.Text;
				obj.endJackpotPoint = chkendJackpotPoint.Checked ? "1" : "0";
				obj.Repeat = txtRepeat.Text;
				obj.ord = txtord.Text != "" ? txtord.Text : "1";
				obj.staid = ddltbStates.SelectedValue;
				tbReadingConfigService.tbReadingConfig_Update(obj);
				BindGrid();
				pnView.Visible = true;
				pnUpdate.Visible = false;
			}
		}

		protected void Back_Click(object sender, EventArgs e)
		{
			pnView.Visible = true;
			pnUpdate.Visible = false;
		}
	}
}